﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Wjtour.Common;

namespace Wjtour.BLL
{
    public class media_bll : base_dll
    {
        public List<Wjtour.Domain.media> GetList(string q, int top)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and mediatitle like '%" + Utility.GetSafeSql(q) + "%'";
            string sql = string.Format(@"select top {0} * from media 
                {1} 
                order by id desc", top, where);
            return DbAccess.GetObjects<Wjtour.Domain.media>(sql).ToList();
        }

        public PaginationData<Wjtour.Domain.media> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and mediatitle like '%" + Utility.GetSafeSql(q) + "%'";
            string sql = string.Format(@"select * from media 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.media> data = GetPaginationData<Wjtour.Domain.media>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.media Load(int id)
        {
            string sql = "select * from media where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.media>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.media entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE media SET 
                mediatitle = @mediatitle,
                imgpath = @imgpath,
                mediadesc = @mediadesc,
                mediapath = @mediapath,
                linkpath = @linkpath,
                sort = @sort,
                createdate = @createdate,
                isactive = @isactive
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO media( 
                mediatitle,imgpath,mediadesc,mediapath,linkpath,sort,createdate,isactive)VALUES(
                @mediatitle,@imgpath,@mediadesc,@mediapath,@linkpath,@sort,@createdate,@isactive)";
            }
            DbAccess.AddInParameter(cmd, "@mediatitle", DbType.String, entity.mediatitle);
            DbAccess.AddInParameter(cmd, "@imgpath", DbType.String, entity.imgpath);
            DbAccess.AddInParameter(cmd, "@mediadesc", DbType.String, entity.mediadesc);
            DbAccess.AddInParameter(cmd, "@mediapath", DbType.String, entity.mediapath);
            DbAccess.AddInParameter(cmd, "@linkpath", DbType.String, entity.linkpath);
            DbAccess.AddInParameter(cmd, "@sort", DbType.Int32, entity.sort);
            DbAccess.AddInParameter(cmd, "@createdate", DbType.DateTime, entity.createdate);
            DbAccess.AddInParameter(cmd, "@isactive", DbType.Int32, entity.isactive);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;

        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from media where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
